﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Components_Report_RevenueForShop : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int ShopID = Session["ShopID"] == null ? 0 : int.Parse(Session["ShopID"].ToString());
            //Load tinh thanh
            int ProvinceID = Session["ProvinceID"] == null ? 0 : int.Parse(Session["ProvinceID"].ToString());
            int isProvinceAll = (ProvinceID == 0 ? 1 : 0);
            Utils.loadLocation(cboProvince, 0, isProvinceAll, ProvinceID);
            //load Shop
            //int ShopID = Session["ShopID"] == null ? 0 : int.Parse(Session["ShopID"].ToString());
            int isShopAll = (ShopID == 0 ? 1 : 0);
            Utils.loadShopByProvinceToCombo(cboShop, isShopAll, ProvinceID, ShopID, "");


            gv.Visible = false;
            //DateTime fromDate  = DateTime.Now.;
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        gv.Visible = true;

        DataTable dt = LoadData();
        gv.DataSource = dt;
        gv.DataBind();

    }
    protected void btnExportExcel_Click(object sender, EventArgs e)
    {
        ExportExcel();
    }
    protected void ExportExcel()
    {
        DataTable tblresult = LoadData();
        if (tblresult.Rows.Count > 0)
        {
            ExcelUtil.ExportDataTableToExcel2007FromRadGrid(tblresult, gv, "doanhthutheocuahang");
        }
        else
        {
            JavascriptUtil.LoadMessage(Page, false, "Không có dữ liệu xuất Excel !!!");

        }
    }
    protected DataTable LoadData()
    {
        DataTable tblResult = new DataTable();
        try
        {
            DateTime FromDate;
            if (dtpFromDate.SelectedDate == null)
            {
                FromDate = DateTime.Parse("2014-01-01");
            }
            else
            {
                FromDate = dtpFromDate.SelectedDate.Value;
            }
            DateTime ToDate;
            if (dtpToDate.SelectedDate == null)
            {
                ToDate = DateTime.Parse("2099-01-01");
            }
            else
            {
                ToDate = dtpToDate.SelectedDate.Value;
            }
            int ProvinceID = int.Parse(this.cboProvince.SelectedValue == "" ? "0" : this.cboProvince.SelectedValue);
            DBDataContext db = LINQUtil.GetDBWriteOnly();
            tblResult = db.Report_RevenueForShopSO(ProvinceID, int.Parse(this.cboShop.SelectedValue), FromDate, ToDate).ToList().ToDataTable();
        }
        catch (Exception ex)
        {
            ControlUtil.LogError("----------------------------");
            ControlUtil.LogError("RevenueForShop-LoadData");
            ControlUtil.LogError(ex.ToString());
            ControlUtil.LogError("----------------------------");
        }
        return tblResult;
    }

    protected void cboProvince_SelectedIndexChanged(object sender, EventArgs e)
    {
        int ProvinceID = int.Parse(this.cboProvince.SelectedValue == "" ? "0" : this.cboProvince.SelectedValue);
        Utils.loadShopByProvinceToCombo(cboShop, 1, ProvinceID, 0, "");
    }
}